package com.example.demo.service.impl;

import com.example.demo.model.Category;
import com.example.demo.model.Drink;
import com.example.demo.repository.CategoryRepository;
import com.example.demo.repository.DrinkRepository;
import com.example.demo.service.IDrinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DrinkJpaService implements IDrinkService {
    @Autowired
    public DrinkRepository drinkRepository;

    @Autowired
    public CategoryRepository categoryRepository;

    @Override
    public Page<Drink> getAllDrinks(PageRequest pageRequest){
        return drinkRepository.findAll(pageRequest);
    }
    @Override
    public void addNewDrink(Drink drink,Integer categoryId){
//        从category中查询出id为categoryId的category
        Category category = categoryRepository.findById(categoryId).orElse(null);
        drink.setCategory(category);
        drinkRepository.save(drink);
    }

    @Override
    public void deleteDrink(Integer id) {
        drinkRepository.deleteById(id);
    }

    @Override
    public Drink getDrinkById(Integer id) {
        return drinkRepository.findById(id).orElse(null);
    }

    @Override
    public void updateDrink(Drink drink) {
        drinkRepository.save(drink);
    }

//    下订单的方法
    @Override
    public List<Drink> getDrinkById(List<Integer> items) {
        return drinkRepository.findDistinctByIdIn(items);
    }


}
